﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using UnityEngine.Graphics;
using Microsoft.Xna.Framework.Graphics;

namespace UnityEngine.Terrain
{
    public class Tree
    {
        public Tree(WCLoader loader)
        {
            this.TreeType = loader.ReadString(4);
            this.Variation = loader.ReadInt32();
            this.Position = loader.ReaderVector3();
            this.RotationAngle = loader.ReadFloat();
            this.Scale = loader.ReaderVector3();
            this.Flag = loader.ReadByte();
            this.TreeLife = loader.ReadByte();
            this.ItemTable = loader.ReadInt32();
            Debug.Assert(this.ItemTable == -1);
            this.ItemCount = loader.ReadInt32();
            Debug.Assert(this.ItemCount == 0);
            this.TreeId = loader.ReadInt32();

            this.IsTree = DoodadPaletteRendererContainer.IsTree(this);
        }

        public string TreeType;
        public int Variation;
        public Vector3 Position;
        public float RotationAngle;
        public Vector3 Scale;
        public byte Flag;
        public byte TreeLife;
        public int ItemTable;
        public int ItemCount;
        public int TreeId;
        public bool IsTree;

        public void PrintDebugInfo()
        {
            Debug.Write(TreeId + " V " + Variation + " F " + Flag + " L " + TreeLife);
            Debug.WriteLine("");
        }
    }

    public class WarCraftMapDoo
    {
        private Landscape _Landscape = null;
        public WarCraftMapDoo(Landscape owner, WCLoader loader)
        {
            this._Landscape = owner;

            loader.ExpectTag("W3do");
            Version = loader.ReadInt32();
            Debug.Assert(Version == 8);
            SubVersion = loader.ReadInt32();
            Debug.Assert(SubVersion == 0x0000000B);
            TreeCount = loader.ReadInt32();

            Trees = new Tree[TreeCount];
            for (int i = 0; i < Trees.Length; i++)
            {
                Trees[i] = new Tree(loader);
                this._Landscape.WarCraftMapPath.SetTree(Trees[i]);
            }

            this._Landscape.WarCraftMapPath.PrepareForPathSearch();

            Debug.Assert(loader.ReadInt32() == 0);
            Debug.Assert(loader.ReadInt32() == 0);
        }

        public void GenerateGraphics()
        {
            PaletteRendererManager manager = SceneManager.Instance.PaletteRendererManager;
            for (int i = 0; i < _Landscape.WarCraftMapDoo.TreeCount; i++)
            {
                var Tree = _Landscape.WarCraftMapDoo.Trees[i];
                Texture2D texture;

                Vector3 Position = new Vector3(Tree.Position.Y - _Landscape.WarCraftEnvironment.CenterOffsetY,
                               Tree.Position.Z,
                               Tree.Position.X - _Landscape.WarCraftEnvironment.CenterOffsetX);

                string TreeName = DoodadPaletteRendererContainer.GetName(Tree, Position, out texture);

                if (TreeName != null)
                {
                    Vector3 Rotation = new Vector3(Tree.RotationAngle - (float)Math.PI / 2, -(float)Math.PI / 2.0f, 0);
                    DoodadInstance Instance = new DoodadInstance(Position, Tree.Scale, Rotation,
                        ERenderState.Opaque | ERenderState.EnableDepth | ERenderState.CullCounterClockWise | ERenderState.Solid,
                        manager.GetPaletteRenderer(EPaletteRendererType.Doodad, TreeName +".mdx") as DoodadRenderer);
                    Instance.IsTree = Tree.IsTree;
                    Instance.IsCreateShadow = !Tree.IsTree;
                    Instance.ReplacableTexture = texture;
                    SceneManager.Instance.AddInstance(Instance);
                    SceneManager.Instance.AddUpdateInstance(Instance);
                }
            }
        }

        public int Version;
        public int SubVersion;
        public int TreeCount;
        public Tree[] Trees;
    }
}
